package cn.dankal.flyaudio.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.R;

public class CancelOrderDialog extends DialogFragment implements View.OnClickListener {

    private View rootView;

    private LinearLayout conditionList1;
    private LinearLayout conditionList2;
    private LinearLayout conditionList3;
    private LinearLayout conditionList4;
    private LinearLayout conditionList5;

    private DoSubmitListener listener;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        rootView = inflater.inflate(R.layout.dialog_cancel_order, container, false);
        rootView.findViewById(R.id.iv_cancel).setOnClickListener(this);
        conditionList1 = rootView.findViewById(R.id.ll_condition_list_1);
        conditionList2 = rootView.findViewById(R.id.ll_condition_list_2);
        conditionList3 = rootView.findViewById(R.id.ll_condition_list_3);
        conditionList4 = rootView.findViewById(R.id.ll_condition_list_4);
        conditionList5 = rootView.findViewById(R.id.ll_condition_list_5);
        rootView.findViewById(R.id.tv_submit).setOnClickListener(this);
        conditionList1.setOnClickListener(this);
        conditionList2.setOnClickListener(this);
        conditionList3.setOnClickListener(this);
        conditionList4.setOnClickListener(this);
        conditionList5.setOnClickListener(this);
        return rootView;
    }

    public void setListener(DoSubmitListener listener) {
        this.listener = listener;
    }

    private String getReason() {
        if (conditionList1.isSelected()) {
            TextView textView = (TextView) conditionList1.getChildAt(1);
            return textView.getText().toString();
        }
        if (conditionList2.isSelected()) {
            TextView textView = (TextView) conditionList2.getChildAt(1);
            return textView.getText().toString();
        }
        if (conditionList3.isSelected()) {
            TextView textView = (TextView) conditionList3.getChildAt(1);
            return textView.getText().toString();
        }
        if (conditionList4.isSelected()) {
            TextView textView = (TextView) conditionList4.getChildAt(1);
            return textView.getText().toString();
        }
        if (conditionList5.isSelected()) {
            TextView textView = (TextView) conditionList5.getChildAt(1);
            return textView.getText().toString();
        }
        return "";
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.iv_cancel:
                dismiss();
                break;
            case R.id.tv_submit:
                if (listener != null) {
                    if (TextUtils.isEmpty(getReason())) {
                        ToastUtils.showShort("请选择取消理由");
                        return;
                    } else {
                        dismiss();
                        listener.doSubmit(getReason());
                    }
                }
                break;
            case R.id.ll_condition_list_1:
                conditionList1.setSelected(true);
                conditionList2.setSelected(false);
                conditionList3.setSelected(false);
                conditionList4.setSelected(false);
                conditionList5.setSelected(false);
                break;
            case R.id.ll_condition_list_2:
                conditionList1.setSelected(false);
                conditionList2.setSelected(true);
                conditionList3.setSelected(false);
                conditionList4.setSelected(false);
                conditionList5.setSelected(false);
                break;
            case R.id.ll_condition_list_3:
                conditionList1.setSelected(false);
                conditionList2.setSelected(false);
                conditionList3.setSelected(true);
                conditionList4.setSelected(false);
                conditionList5.setSelected(false);
                break;
            case R.id.ll_condition_list_4:
                conditionList1.setSelected(false);
                conditionList2.setSelected(false);
                conditionList3.setSelected(false);
                conditionList4.setSelected(true);
                conditionList5.setSelected(false);
                break;
            case R.id.ll_condition_list_5:
                conditionList1.setSelected(false);
                conditionList2.setSelected(false);
                conditionList3.setSelected(false);
                conditionList4.setSelected(false);
                conditionList5.setSelected(true);
                break;
        }
    }

    public interface DoSubmitListener {
        void doSubmit(String reason);
    }
}
